package hibernate;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Set;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import controller.CountOrder;

/**
 * A data access object (DAO) providing persistence and search support for
 * Orderstab entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see hibernate.Orderstab
 * @author MyEclipse Persistence Tools
 */
public class OrderstabDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(OrderstabDAO.class);
	// property constants
	public static final String PRICE = "price";
	public static final String ORDER_TIME = "orderTime";
	public static final String ORDER_NUMBER = "orderNumber";
	public static final String STATE = "state";

	public void save(Orderstab transientInstance) {
		log.debug("saving Orderstab instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Orderstab persistentInstance) {
		log.debug("deleting Orderstab instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Orderstab findById(java.lang.Integer id) {
		log.debug("getting Orderstab instance with id: " + id);
		try {
			Orderstab instance = (Orderstab) getSession().get(
					"hibernate.Orderstab", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Orderstab instance) {
		log.debug("finding Orderstab instance by example");
		try {
			List results = getSession().createCriteria("hibernate.Orderstab")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Orderstab instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Orderstab as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}

	public List findByOrderTime(Object orderTime) {
		return findByProperty(ORDER_TIME, orderTime);
	}

	public List findByOrderNumber(Object orderNumber) {
		return findByProperty(ORDER_NUMBER, orderNumber);
	}

	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findAll() {
		log.debug("finding all Orderstab instances");
		try {
			String queryString = "from Orderstab";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Orderstab merge(Orderstab detachedInstance) {
		log.debug("merging Orderstab instance");
		try {
			Orderstab result = (Orderstab) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Orderstab instance) {
		log.debug("attaching dirty Orderstab instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Orderstab instance) {
		log.debug("attaching clean Orderstab instance");
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	//查询订单,可设置订单类型
	public List<Orderstab> queryOrder(int parkId,int state){
		
		log.debug("finding urtreated order instance");
		try {
			//String hqlString = "from Orderstab as a where a.state=0 and a.parkstab = :parkId";
			String hqlString = "from Orderstab as a where a.state=:state and a.parkstab = :parkId";
			Query queryObject = getSession().createQuery(hqlString);
			//queryObject.setParameter("parkId", parkId);
			queryObject.setInteger("parkId", parkId);
			queryObject.setInteger("state", state);
			List<Orderstab> list = queryObject.list();
			if(list !=null && list.size() > 0){               
				return list;
			}else {
				return null;
			}
		} catch (RuntimeException re) {                       
			log.error("find  failed", re);
			throw re;
		}
	}
	
	public List countOrders(int parkId,int y,int m,int d){
		log.debug("count orders");
		try{
			if(m==13 && d==0){
				String sqlString="select count(*) as totalOrders,sum(price)as totalPrice,"
						+ " year(pushTime) as year, month(pushTime)as month"
						+ " from orderstab where year(pushTime)=:y and parkUuid=:parkId group by month";
				Query queryObject = getSession().createSQLQuery(sqlString);
				queryObject.setInteger("y",y);
				queryObject.setInteger("parkId",parkId);
				List list=queryObject.list();
				return list;
				}else if(m !=0 && d==0){
					String sqlString="select count(*) as totalOrders,sum(price)as totalPrice,"
							+ " year(pushTime) as year, month(pushTime)as month,day(pushTime) as date"
							+ " from orderstab where year(pushTime)=:y and month(pushTime)=:m and parkUuid=:parkId group by month";
					Query queryObject = getSession().createSQLQuery(sqlString);
					queryObject.setInteger("y",y);
					queryObject.setInteger("m",m);
					queryObject.setInteger("parkId",parkId);
					List list=queryObject.list();
					return list;
				}else{
					String sqlString="select count(*) as totalOrders,sum(price)as totalPrice,"
							+ "year(pushTime) as year, month(pushTime)as month,day(pushTime)"
							+ " from orderstab where year(pushTime)=:y and month(pushTime)=:m and day(pushTime)= :d and parkUuid=:parkId";
					Query queryObject = getSession().createSQLQuery(sqlString);
					queryObject.setInteger("y",y);
					queryObject.setInteger("m",m);
					queryObject.setInteger("d",d);
					queryObject.setInteger("parkId",parkId);
					List list= queryObject.list();
					return list;
				}
			}catch (RuntimeException re) {
				log.error("attach failed", re);
				throw re;
			}
	}

}